using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace FileTracker.Forms
{
    public partial class FormEditOpenWith : Form
    {
        ListViewItem _currentItem;
        //ApplicationSettings _configFile;
        bool _isEditMode = false;

        public FormEditOpenWith()
        {
            InitializeComponent();
        }

        private void EditOpenWith_Load(object sender, EventArgs argEventArgs)
        {
            ColumnHeader columnHeaderName;
            ColumnHeader columnHeaderProgram;
            
            listViewOpenWith.View = View.Details;
            listViewOpenWith.FullRowSelect = true;
            listViewOpenWith.CheckBoxes = false;

            columnHeaderName = new ColumnHeader();
            columnHeaderName.Text = "Name";
            columnHeaderName.Width = 90;
            columnHeaderName.TextAlign = HorizontalAlignment.Left;

            columnHeaderProgram = new ColumnHeader();
            columnHeaderProgram.Text = "Program";
            columnHeaderProgram.Width = 225;
            columnHeaderProgram.TextAlign = HorizontalAlignment.Left;

            listViewOpenWith.Columns.Add(columnHeaderName);
            listViewOpenWith.Columns.Add(columnHeaderProgram);

            //ApplicationSettings
            //_configFile = new ApplicationSettings();
	        IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            //Loop through Favorites
			foreach (KeyValuePair<string, string> CurrentOpenWith in localSettings.ProgramList)
            {
                ListViewItem itemNew;

                string OpenWithName = CurrentOpenWith.Key; 
                string OpenWithProgram = CurrentOpenWith.Value;

                itemNew = new ListViewItem(OpenWithName);
                itemNew.SubItems.Add(OpenWithProgram);

                listViewOpenWith.Items.Add(itemNew);
            }

        }

        private void buttonEdit_Click(object sender, EventArgs argEventArgs)
        {
            if (listViewOpenWith.SelectedItems.Count <= 0)
                return;

            _currentItem = listViewOpenWith.SelectedItems[0];

            textBoxName.Text = _currentItem.SubItems[0].Text;
            textBoxProgram.Text = _currentItem.SubItems[1].Text;

            _isEditMode = true;
        }

        private void buttonOK_Click(object sender, EventArgs argEventArgs)
        {
            if (textBoxProgram.Text == "")
                return;

            //Check for program exists
            if (!File.Exists(textBoxProgram.Text))
            {
                MessageBox.Show("That program does not exist.  Please try again.", "Invalid Open With program");
                textBoxProgram.Focus();
                return;
            }

            //Check if user is editing an old entry
            if (_isEditMode)
            {
                _currentItem.SubItems[0].Text = textBoxName.Text;
                _currentItem.SubItems[1].Text = textBoxProgram.Text;

                _isEditMode = false;
                _currentItem = null;
                textBoxName.Text = "";
                textBoxProgram.Text = "";

                return;
            }

            //TO DO:Check for duplicates
            foreach(ListViewItem CurrentItem in listViewOpenWith.Items)
            {
                //Check the name
                if (textBoxName.Text == CurrentItem.SubItems[0].Text)
                {
                    MessageBox.Show("There is already an Open With with that name.  Please try again.", "Invalid Open With name");
                    textBoxName.Focus();
                    return;
                }

                //Check the path
                if (textBoxProgram.Text == CurrentItem.SubItems[1].Text)
                {
                    MessageBox.Show("There is already an Open With for that program.  Please try again.", "Invalid Open With program");
                    textBoxProgram.Focus();
                    return;
                }
            }

            ListViewItem itemNew;

            itemNew = new ListViewItem(textBoxName.Text);
            itemNew.SubItems.Add(textBoxProgram.Text);

            listViewOpenWith.Items.Add(itemNew);

            _isEditMode = false;
            _currentItem = null;
            textBoxName.Text = "";
            textBoxProgram.Text = "";
        }

        private void buttonSave_Click(object sender, EventArgs argEventArgs)
        {

            if (_isEditMode)
            {
                MessageBox.Show("You are still in edit mode.  Please finish editing or clear the selection.");
                return;
            }
	        IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

			localSettings.ProgramList.Clear();

            //Loop through ListViewItems
            foreach (ListViewItem CurrentItem in listViewOpenWith.Items)
            {
				localSettings.ProgramList.Add(CurrentItem.SubItems[0].Text, CurrentItem.SubItems[1].Text);
            }

            //Save the config file.
			localSettings.Save();

            this.Close();

        }

        private void buttonDelete_Click(object sender, EventArgs argEventArgs)
        {
            if (listViewOpenWith.SelectedItems.Count <= 0)
                return;

            listViewOpenWith.SelectedItems[0].Remove();

            _currentItem = null;
            textBoxName.Text = "";
            textBoxProgram.Text = "";
            _isEditMode = false;
        }

        private void buttonClear_Click(object sender, EventArgs argEventArgs)
        {
            _currentItem = null;

            textBoxName.Text = "";
            textBoxProgram.Text = "";
            _isEditMode = false;
        }

        private void buttonPathBrowseProgram_Click(object sender, EventArgs argEventArgs)
        {
            OpenFileDialog BrowseDefault;
            BrowseDefault = new OpenFileDialog();
            BrowseDefault.CheckFileExists = true;
            BrowseDefault.DereferenceLinks = true;
            BrowseDefault.Multiselect = false;
            BrowseDefault.SupportMultiDottedExtensions = true;
            BrowseDefault.Title = "Select Program";
            BrowseDefault.ValidateNames = true;

            BrowseDefault.ShowDialog(this);
            textBoxProgram.Text = BrowseDefault.FileName;//.SelectedPath;

        }

        private void buttonCancel_Click(object sender, EventArgs argEventArgs)
        {
            this.Close();
        }
    }
}